import { _decorator, assetManager, Component, director, find, ImageAsset, Node, Sprite, SpriteFrame, sys, Texture2D } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Permanent')
export class Permanent extends Component {
    //保存头像
    WXAvatar:SpriteFrame = null

    // 滑块移速，触摸灵敏度灵敏度,默认为5
    silderspead:number = 5

    levelnum:number = 0

    // 更新灵敏度方法
    speedUpdate(speed:number){
      this.silderspead = speed
      sys.localStorage.setItem('silderspead',speed.toString())
    }

    start() {
        //设置为常驻节点
        director.addPersistRootNode(this.node)
        // 检测本地缓存关卡数据，有则设置成本地数据，无初始化为1
        if(sys.localStorage.getItem('level')){
            this.levelnum = Number(sys.localStorage.getItem('level'))
        }else{
            sys.localStorage.setItem('level',this.levelnum.toString())
        }

        //检测本地灵敏度设置，有设置全局为本地灵敏度，无设置本地为5
        if(sys.localStorage.getItem('silderspead')){
          this.silderspead = Number(sys.localStorage.getItem('silderspead'))
        }else{
            sys.localStorage.setItem('silderspead',this.silderspead.toString())
        }
        

        wx.getUserInfo({
            success(res){
              assetManager.loadRemote(res.userInfo.avatarUrl, { ext: '.png' }, (err, data: ImageAsset) => {
                let sp: SpriteFrame = new SpriteFrame();
                let tx: Texture2D = new Texture2D();
                tx.image = data;
                sp.texture = tx
                find('Permanent').getComponent(Permanent).WXAvatar = sp
                find("Canvas/weixin").getComponent(Sprite).spriteFrame = sp
              }) 
              console.log(res.userInfo.avatarUrl)
            },
            fail(){
              //创建微信授权按钮
              let indosync = wx.getSystemInfoSync()
              let screenWidh = indosync.screenWidth
              let screenHeight = indosync.screenHeight
              const button = wx.createUserInfoButton({
                type: 'text',
                text: '获取用户信息',
                style: {
                  left: 0,
                  top: 0,
                  width: screenWidh,
                  height: screenHeight,
                  lineHeight: 40,
                  backgroundColor: '#00000000',
                  color: '#ffffff',
                  textAlign: 'center',
                  fontSize: 0,
                  borderRadius: 4
                }
              })
                //设置全局信息
              button.onTap((res) => {
                if(res.errMsg=="getUserInfo:ok"){
                  //远程图片加载与转换
                  assetManager.loadRemote(res.userInfo.avatarUrl, { ext: '.png' }, (err, data: ImageAsset) => {
                  let sp: SpriteFrame = new SpriteFrame();
                  let tx: Texture2D = new Texture2D();
                  tx.image = data;
                  sp.texture = tx;
                  find('Permanent').getComponent(Permanent).WXAvatar = sp
                  find("Canvas/weixin").getComponent(Sprite).spriteFrame = sp
                  })
                  button.destroy()
                }else{
                  console.log("授权失败")
                }
              })
            }
          })
    }

    update(deltaTime: number) {
    }
}


